package hu.tortavar.client.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.ui.HTML;

public class OpeningHoursData {
	private static List<DayInfo> days = new ArrayList<DayInfo>();
	private String[] DAYS = {"Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"};
	
	public class DayInfo {
		
		private int dayOfWeek;
		private boolean isOpenToday;
		private String from; //hh:mm
		private String to;	 //hh:mm
		
		public DayInfo(boolean isOpenToday, int dayOfWeek, String from, String to) {
			this.isOpenToday = isOpenToday;
			this.dayOfWeek = dayOfWeek;
			this.from = from;
			this.setTo(to);
		}

		public boolean isOpenToday() {
			return isOpenToday;
		}

		public void setOpenToday(boolean isOpenToday) {
			this.isOpenToday = isOpenToday;
		}

		public String getFrom() {
			return from;
		}

		public void setFrom(String from) {
			this.from = from;
		}

		public String getTo() {
			return to;
		}

		public void setTo(String to) {
			this.to = to;
		}

		public int getDayOfWeek() {
			return dayOfWeek;
		}

		public void setDayOfWeek(int dayOfWeek) {
			this.dayOfWeek = dayOfWeek;
		}
		
	}
	
	public OpeningHoursData() {
		
		days.add(new DayInfo(false, 0, "", "")); // Sunday
		days.add(new DayInfo(false, 1, "", "")); // Monday
		days.add(new DayInfo(true, 2, "10:00", "18:00")); // Tuesday
		days.add(new DayInfo(true, 3, "10:00", "18:00")); // Wednesday
		days.add(new DayInfo(true, 4, "10:00", "18:00")); // Thursday
		days.add(new DayInfo(true, 5, "10:00", "18:00")); // Friday
		days.add(new DayInfo(true, 6, "10:00", "14:00")); // Saturday
	}
	
	/**
	 * 
	 * @param cal
	 * A datum alapján megállapítja a nyitvatartást 
	 * cal.get(int field) 9 DAY_OF_WEEK
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public boolean isOpenNow(){
		Date now = new Date();
		DayInfo today = days.get(now.getDay());
		if (today.isOpenToday) {
			final Date from = new Date();
			final Date to = new Date();
			from.setHours(Integer.parseInt(today.getFrom().substring(0,2)));
			from.setMinutes(Integer.parseInt(today.getFrom().substring(3)));
			//from.set(Calendar.HOUR_OF_DAY, Integer.parseInt(today.getFrom().substring(0,2)));
			//from.set(Calendar.MINUTE, 0);
			to.setHours(Integer.parseInt(today.getTo().substring(0,2)));
			to.setMinutes(Integer.parseInt(today.getTo().substring(3)));
			//to.set(Calendar.HOUR_OF_DAY, Integer.parseInt(today.getTo().substring(0,2)));
			//to.set(Calendar.MINUTE, 0);
			
			if (now.after(from) && now.before(to)) {
				return true;
			}
		}
		return false;
		}
	
	public String toHTML(){
		String openHours = "";
		
		for (int i = 1; i<=6; ++i) {
			if (days.get(i).isOpenToday) {
				openHours += DAYS[i] + " " + days.get(i).getFrom() + " - " + days.get(i).getTo() + "<br>";
			} else {
				openHours += DAYS[i] + " " + "Zárva<br>";
			}
		}
		
		if(days.get(0).isOpenToday) {
			openHours += DAYS[0] + " " + days.get(0).getFrom() + " - " + days.get(0).getTo() + "<br>";
		} else {
			openHours += DAYS[0] + " " + "Zárva<br>";
		}
		
		return openHours;
	}
}
